from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Post(models.Model):
	created_at = models.DateTimeField(auto_now_add=True)
	pubdate = models.DateTimeField(null=True,blank=True)
	body = models.TextField()
	tags = models.ManyToManyField(Tag)
	slug = models.SlugField()
	source = models.CharField(max_length=50,null=True,blank=True)
	source_link = models.URLField(null=True,blank=True)
	source_id = models.CharField(max_length=50,unique=True,null=True,blank=True)

	@models.permalink
	def get_absolute_url(self):
	    return ('farm.views.post', [str(self.id)])

	def pubdate_for_robots(self):
		dt = self.pubdate
		if dt == None:
			dt = self.created_at
		if dt == None:
			return ""

		return dt.strftime('%Y-%m-%dT%H:%M:%S%z')
		
	def pubdate_for_humans(self):
		from datetime import datetime, timedelta
		dt = self.pubdate
		curr = datetime.now()
		if dt == None:
			dt = self.created_at
		if dt == None:
			return ""

		# set up some deltas for comparison
		min = timedelta(minutes=1)
		hour = timedelta(hours=1)
		day = timedelta(days=1)
		delta = curr - dt
		
		if delta < min: return "Less than a minute ago"
		if delta < hour: return "About %s minutes ago" % delta.minutes
		if delta < day: return "About %s hours ago" % delta.hours
		if delta < (day * 4): return "About %s days ago" % delta.days
		if delta > (day * 3): return dt.strftime("%H:%M on %d %B %Y")

		# in case of wierdness, say nothing.
		reutrn ""
	
class Tag(models.Model):
	name = models.CharField(max_length=20)
	major = models.BooleanField(default=False)
	